package top;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * 四数相加: 哈希
 *
 * @author chenyw
 * @date 2022/7/22 20:31
 */
public class Top454fourSumCount {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();
        int n = nums1.length;
        int tmp;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                tmp = nums1[i] + nums2[i];
                map.put(tmp, map.getOrDefault(tmp, 0) + 1);
            }
        }
        int res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                tmp = 0 - (nums3[i] + nums4[i]);
                if (map.containsKey(tmp)) {
                    res += map.get(tmp);
                }
            }
        }
        return res;
    }
}
